<template>
  <el-drawer
    v-if="visible"
    :visible.sync="visible"
    :with-header="false"
    :append-to-body="true"
    size="1200px"
  >
    <div class="title">
      <div class="title-text">提瓶充装单详情{{ fillingInfo.fillingCode }}</div>
      <el-tag :type="fillingInfo.fillingStatus===1?'success':'danger'" class="tag">{{ formatColumnMap(fillingStatusMap, 'value', fillingInfo.fillingStatus, 'desc') }}</el-tag>
    </div>
    <div class="center">
      <div class="c-content">
        <div class="item-title">基础信息：</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="grid-content">
              <span>提瓶充装单号：</span><span>{{ fillingInfo.fillingCode }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content">
              <span>创建时间：</span><span>{{ fillingInfo.transactTime && parseTime(fillingInfo.transactTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content">
              <span>状态：</span><span :class="fillingInfo.fillingStatus&&fillingInfo.fillingStatus===2?'item-red':'item-green'">{{ fillingInfo.fillingStatus?fillingInfo.fillingStatus===1?'正常':fillingInfo.fillingStatus===2?'作废':'':'' }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content">
              <span>提瓶充装机构：</span><span>{{ fillingInfo.operateOrgName }}</span>
            </div>
          </el-col>
        </el-row>
        <div class="item-title">提瓶人信息：</div>
        <div v-if="fillingInfo.withdrawType === 2">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="grid-content">
                <span>提瓶人：</span><span>{{ fillingInfo.withdrawUser }}</span><span style="color: red;">(用户)</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content">
                <span>用户编号：</span><span>{{ fillingInfo.withdrawCode }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content">
                <span>用户类型：</span><span>{{ fillingInfo.csmType? formatColumnMap(customerTypeMap, 'value', fillingInfo.csmType, 'desc'):'' }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="grid-content">
                <span>联系电话：</span><span>{{ fillingInfo.csmRgPhone }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content">
                <span>单据标签：</span><span>{{ fillingInfo.fillingLabelName || '' }}</span>
              </div>
            </el-col>
          </el-row>
        </div>
        <div v-if="fillingInfo.withdrawType === 1">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="grid-content">
                <span>提瓶人：</span><span>{{ fillingInfo.withdrawUser }}</span><span style="color: red;">(配送员)</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content">
                <span>所属机构：</span><span>{{ fillingInfo.orgName }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content">
                <span>单据标签：</span><span>{{ fillingInfo.fillingLabelName || '' }}</span>
              </div>
            </el-col>
          </el-row>
        </div>

        <div class="item-title">提瓶充装明细：</div>
        <el-table
          :data="tableData"
          :summary-method="getSummaries"
          show-summary
          style="width: 100%;"
        >
          <el-table-column
            prop="goodsName"
            label="商品信息"
            width="180"
          />
          <el-table-column
            prop="specAlias"
            label="规格"
            width="140"
          />
          <el-table-column
            prop="weight"
            label="重量(kg)"
          />
          <el-table-column
            prop="gPrice"
            label="商品单价(元)"
          />
          <el-table-column
            prop="goodsCount"
            label="数量(瓶)"
          />
          <el-table-column
            prop="gPrefPrice"
            label="优惠金额(元)"
          />
          <el-table-column
            prop="totalPrice"
            label="总金额(元)"
          />
        </el-table>
      </div>
      <div class="right">
        <div class="timeline-title">流转信息</div>
        <el-timeline>
          <el-timeline-item v-for="(item, index) in flowInfoData" :key="index" :timestamp="`${parseTime(item.happendDt, '{y}-{m}-{d} {h}:{i}')} ${item.operator}`" placement="top">
            <el-card>
              <h4>{{ item.traceDesc }}</h4>
              <h4 v-if="item.marker">{{ `${item.marker}:${item.other}` }}</h4>
              <p>{{ `发生部门&nbsp;——&nbsp;${item.orgName ? item.orgName : '系统'}` }}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>

    </div>

    <!-- 底部 -->
    <div class="demo-drawer__footer" style="padding: 0 10px 10px 0; text-align: right;">
      <div class="btn-group">
        <div class="btns">
          <el-button v-if="fillingInfo.orderStatus !== -1 && fillingInfo.orderStatus !== 2" v-permission="4" type="primary" @click="$emit('groovePrint', fillingInfo);visible=false;"> 打印 </el-button>
          <el-button v-if="fillingInfo.fillingStatus === 1" v-permission="5" type="danger" @click="$emit('del', fillingInfo);visible=false;">作废</el-button>
          <el-button v-permission="7" type="primary" @click="$emit('labelEditSingle', [fillingInfo.fillingId]);"> 标签修改 </el-button>
        </div>
        <el-button @click="visible = false"> 关闭 </el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import { formatColumnMap, parseTime } from '@/utils'
  import { getfillingDetail, queryOrderFlowInfoCommon } from '@/api'
  import { getSummaries } from '../js/summary'
  export default {
    name: 'fillingDetail',
    props: {
      fillingStatusMap: {
        type: Array,
        default: () => []
      },
      customerTypeMap: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        visible: false,
        currentRow: {},
        tableData: [],
        fillingInfo: {},
        flowInfoData: []
      }
    },
    methods: {
      formatColumnMap, parseTime, getSummaries,
      open(row) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (row) {
            this.currentRow = Object.assign(this.currentRow, row)
            this.getDetail()
            this.getFlowInfoData()
          }
        })
      },
      async getDetail() {
        const params = {
          fillingId: this.currentRow.fillingId
        }
        const res = await getfillingDetail(params)
        if (res && res.data) {
          this.fillingInfo = res.data
          this.tableData = res.data.detailList.map((item) => ({
            ...item,
            weight: item.weight ? item.weight.toFixed(2) : '0.00',
            gPrice: item.gPrice ? item.gPrice.toFixed(2) : '0.00',
            gPrefPrice: item.gPrefPrice ? item.gPrefPrice.toFixed(2) : '0.00',
            totalPrice: item.totalPrice ? item.totalPrice.toFixed(2) : '0.00'
          })) || []
        }
      },
      reset() {
        this.visible = false
        this.currentRow = {}
        this.fillingInfo = {}
      },
      // 充装流转信息
      async getFlowInfoData() {
        const params = {
          billCode: this.currentRow.fillingCode,
          billType: 16384
        }
        const res = await queryOrderFlowInfoCommon(params)
        this.flowInfoData = res.data
      }
    }

  }
</script>

<style lang="scss" scoped>
/deep/.el-drawer__header {
  padding: 15px 20px;
  margin-bottom: 0;
}

.title {
  display: flex;
  padding: 20px 20px 0 20px;
  margin-bottom: 20px;
  .title-text {
    line-height: 25px;
  }
  .tag {
    margin: 0 10px;
  }
}

.c-content {
  padding: 0 20px;
  overflow-y: auto;
  font-size: 14px;
  font-weight: 400;
  .item-title {
    margin: 10px  0;
    color: #01999F;
    &:nth-child(1) {
      margin-top: 0;
    }
    &:nth-last-of-type(1) {
      margin-bottom: 20px;
    }
  }
  .grid-content {
    line-height: 30px;
    .item-red {
      color: red;
    }
    .item-green {
      color: green;
    }
  }
}

.demo-drawer__footer {
  position: fixed;
  bottom: 0;
  width: 900px;
  height: 55px;
  background: white;
  border: 2px solid #DCDFE6;
  .btn-group {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    margin-right: 10px;
    .btns {
      margin-left: 15px;
    }
  }
}

.center {
  display: flex;
  justify-content: space-between;
  height: calc(100vh - 120px);
  .c-content {
    flex: 1;
  }
  .right {
    width: 25%;
    height: 100%;
    padding: 0 15px;
    border-left: 1px solid #e3e3e3;
    .timeline-title {
      width: 100%;
      padding: 8px;
      margin-bottom: 10px;
      color: white;
      background-color: #38BDBD;
      border-radius: 5px;
    }
    .el-timeline {
      margin-top: 20px;
    }
  }
}

</style>
